package org.dftproject.genesis.ui.pages.pedigree.zoom;

import org.dftproject.genesis.ui.Activator;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

public class ZoomButtonFigure extends Figure {
	
	public static final int IN = 0;
	public static final int OUT = 1;
	
	public static final Color backgroundColor = ColorConstants.listBackground;
	public static final Color borderColor = new Color(null, 128, 128, 128);
	
	private static final int inset = 4;
	
	private final int style;
	private final ZoomManager zoomManager;
	private boolean hover;
	
	public ZoomButtonFigure(int style, ZoomManager zoomManager) {
		this.zoomManager = zoomManager;
		this.style = style;
		
		setCursor(Cursors.HAND);
		setRequestFocusEnabled(true);
		addListeners();
		
		this.setLayoutManager(new StackLayout());
		this.setBorder(new MarginBorder(inset));
		
		ImageFigure icon = new ImageFigure(Activator.getImageDescriptor(style == IN ? "icons/magnifier_zoom_in.png" : "icons/magnifier_zoom_out.png").createImage());
		this.add(icon);
	}

	protected void addListeners() {
		MouseTracker tracker = new MouseTracker();
		addMouseMotionListener(tracker);
		addMouseListener(tracker);
	}
	
	protected boolean getHover() {
		return hover;
	}
	
	protected void setHover(boolean hover) {
		this.hover = hover;
	}
	
	protected void setPressed(boolean pressed) {
		if (pressed)
			setBorder(new MarginBorder(inset + 1, inset + 1, inset - 1, inset - 1));
		else
			setBorder(new MarginBorder(inset, inset, inset, inset));
	}
	
	@Override
	public void paint(Graphics graphics) {
		if (hover) {		
			Rectangle bounds = getBounds();
			
			graphics.setBackgroundColor(backgroundColor);
			graphics.fillRoundRectangle(new Rectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1), 4, 4);
			
			graphics.setForegroundColor(borderColor);
			graphics.drawRoundRectangle(new Rectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1), 4, 4);
		}

		super.paint(graphics);
	}

	protected class MouseTracker extends MouseMotionListener.Stub implements MouseListener {
		
		private boolean mouseDown;

		public void mousePressed(MouseEvent me) {
			requestFocus();
			me.consume();

			mouseDown = true;
			setPressed(true);
		}

		public void mouseReleased(MouseEvent me) {	
			mouseDown = false;
			
			if (getBounds().contains(me.getLocation())) {
				if (style == IN)
					zoomManager.setExponent(zoomManager.getExponent() + 1);
				else if (style == OUT)
					zoomManager.setExponent(zoomManager.getExponent() - 1);
			}
			
			setPressed(false);
		}

		public void mouseDoubleClicked(MouseEvent me) {
		}
		
		@Override
		public void mouseDragged(MouseEvent me) {
			boolean mouseOver = getBounds().contains(me.getLocation());
			if (mouseDown)
				setPressed(mouseOver);
			setHover(mouseOver);
			revalidate();
		}
		
		@Override
		public void mouseEntered(MouseEvent me) {
			setHover(true);
			repaint();
		}
		
		@Override
		public void mouseExited(MouseEvent me) {
			setHover(false);
			repaint();
		}

	}

}
